package com.myapi.ModelPoker;
import java.util.*;

import com.myapi.server.PokerServiceImpl;


public class HoldEmDealer extends Dealer {

	int commCardsDealt;

	public HoldEmDealer(String name) {
		super(name);
		commCardsDealt = 0;
	}

	public void startHand(double ante, double smallBlind, double bigBlind) {
		commCardsDealt = 0;
		PokerServiceImpl pokerServerControler = PokerServiceImpl.getpokerServerControler();
		prepareDeck();
		setPlayerHandMucks(false);
		getTable().setCurrentBet(bigBlind);
		if(ante > 0)
			getAntes(ante);
		if(bigBlind > 0)
			getBlinds(smallBlind, bigBlind);
		// gui.updatePlayerChips();
		dealToHands(2);  //HoldEm deals 2 cards to each hand
		//getTable().printPlayerHands();
		pokerServerControler.startPreflop();
	}

	
	private void dealCommunityCard() {
		CardDeck deck = getCardDeck();
		Table table = getTable();
		if(deck.getNumCards() <= 0) {  //sanity check that it is ok to deal a card
			System.out.println("Cannot deal another card, the deck is empty!");
			return;
		}
		int numPlayerHands = table.getNumPlayerHands();
		Card topCard = deck.getTopCard();

		if(topCard != null) {  //if there are still cards in the deck
			for(int i = 0; i < numPlayerHands; i++) {
				table.addCardToHand(topCard, i);
			}
			table.addCommCard(topCard);
			commCardsDealt++;
		} else {
			System.out.println("Ran out of cards in deck - this should not have happened! =( =( =(");
			return;
		}
		PokerServiceImpl pokerServerControler = PokerServiceImpl.getpokerServerControler();
		pokerServerControler.addCommCardCouter(topCard.getValue()+topCard.getSuit());
	}


	public void update(Observable o, Object arg) {
		if(o instanceof TablePot) {
			PokerServiceImpl pokerServerControler = PokerServiceImpl.getpokerServerControler();
			TablePot tP = (TablePot) o;
			int activePlayers = getTable().getNumActivePlayerHands();
			//If there are only one player left the round is finished

			boolean matched = tP.contributionsMatched(activePlayers);
			if(matched) {
				tP.setContributionsComplete();
				if(tP.getPlayerAllIn()) {  //if a player is all in
					boolean sidePotCreate=getTable().createSidePot(tP); 
					//TODO: create side pot display in gui

					//if all the player are all in or there are just one player left who isn't all in we finished the game 
					if(getTable().getNumActivePlayerHands()<=1&&sidePotCreate){
						for(int i=commCardsDealt;i<5;i++) {
							dealCommunityCard();
						}
						endHand();
						return;
					}


				}
				//deal next round
				if(commCardsDealt == 0) {
					getTable().setCurrentBet(0);
					pokerServerControler.startNextBetting();
					dealCommunityCard();
					dealCommunityCard();
					dealCommunityCard();
				} else if(commCardsDealt == 3 || commCardsDealt == 4) {
					getTable().setCurrentBet(0);
					pokerServerControler.startNextBetting();
					dealCommunityCard();
				} else {  //must be at end of round
					endHand();
				}
			} else {
				//  gui.updatePotDisplay(tP.getTotal());
				pokerServerControler.incrementPlayersTurn();  //keep going around table until betting round is complete (matched)
			}            
		}
	}

}
